Introduction to R and RStudio/Posit
Marc Comas-Cufí
ggplot2 (Marc)Console pane to execute instructions interactively.Environment pane to control existing variables.html, css, markdown, python, C/C++, …IRkernel) orWilke (2020). Fundamentals of Data Visualization
Sources with R code at https://github.com/clauswilke/dataviz
help is the primary interface to the help systems.Help pane.source("script_file.R")Rscript script_file.Ratomic’s and list’slogical,integer,numeric,character, and alsoraw and complex.logical \(\rightarrow\) integer \(\rightarrow\) numeric \(\rightarrow\) character<- (most common) o -> operators (least common):NA is a symbol to define a non-available observation.NA:NA or if a vector has some NA:list():list is the data.frame, where elements should have the same length:list’s: we use [[]] to obtain the content of one vector’s element.data.frame’s: we use [rows,columns] to slice a table by rows and columns.queue = c("Maria", "Josep", "Marçal", "Lluïsa") represents a supermarket queue, with Maria first in line. Using R expressions update the supermarket queue:
+), subtraction (-), multiplication (*), division (/), and exponentiation (^).
%%) and integer division (%/%).&), or (|), negation (!)
<), greater than (>), lower or equal than (<=), greater or equal than (>=), equal to (==), not equal to (!=), is in (%in%).Structure:
It is common to avoid the return() statement:
Fibonacci numbers \(F_0, F_1, F_2, \dots\) can be defined recursively as
\[ F_n = \left\{\begin{matrix} n & \text{si $0 \leq n \leq 1$} \\ F_{n-1} + F_{n-2} & \text{si $2 \leq n$} \end{matrix}\right. \]
Recursive implementation:
Iterative implementation:
CRAN and Bioconductor are the main package repositories of R.
Github contains lots of packages.
tidyverse: R packages for data scienceThe tidyverse is an opinionated collection of R packages designed for data science. All packages share an underlying design philosophy, grammar, and data structures.
Install the complete tidyverse with:
dplyr: a grammar of data manipulationtidyr: helpers to create tidy dataforcats)lubridate)stringr)